package com.dyylearn.servlet.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContext_ extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取web.xml的context-parameter

        //1.获取到ServletContext对象
        ServletContext servletContext = getServletContext();
        //2.获取website
        String website = servletContext.getInitParameter("website");
        String company = servletContext.getInitParameter("company");
        //3.获取项目的工程路径
        String contextPath = servletContext.getContextPath();
        //4.获取项目发布后，正在工作的路径
        // /表示我们的项目(发布后)的 根路径 D:\code practice\java-learning-code\javaWeb\servlet02\out\artifacts\servlet02_war_exploded
        String realPath = servletContext.getRealPath("/");
        System.out.println("项目发布后的绝对路径= " + realPath);
        System.out.println("项目路径= " + contextPath);// /servlet02
        System.out.println("website= " + website);
        System.out.println("company= " + company);
    }
}
